<?php
namespace app\common\model;
use think\Model;
use longbingcore\wxcore\WxSetting;
use think\Db;
class Coach extends Model
{
    //定义表名
    protected $name = 'massage_service_coach_list';
    protected $append = [
        'comment_num',
        'collect_num',
        'order_num'
    ];
    /**
     * @author chenniang
     * @DataTime: 2021-07-22 14:33
     * @功能说明:保留两位
     */
    public function getServicePriceAttr($value,$data){
        if(isset($value)){
            return round($value,2);
        }
    }
    /**
     * @author chenniang
     * @DataTime: 2021-07-22 14:33
     * @功能说明:保留两位
     */
    public function getCarPriceAttr($value,$data){
        if(isset($value)){
            return round($value,2);
        }
    }
    /**
     * @author chenniang
     * @DataTime: 2021-07-07 22:47
     * @功能说明:评论数量
     */
    public function getOrderNumAttr($value,$data){
        if(!empty($data['id'])){
            $comm_model = new Order();
            $num = $comm_model->where(['coach_id'=>$data['id'],'pay_type'=>7])->count();
            return $num;
        }
    }
    /**
     * @author chenniang
     * @DataTime: 2021-07-07 22:47
     * @功能说明:评论数量
     */
    public function getCommentNumAttr($value,$data){
        if(!empty($data['id'])){
            $comm_model = new Comment();
            $num = $comm_model->where(['coach_id'=>$data['id'],'status'=>1])->count();
            return $num;
        }
    }
    /**
     * @author chenniang
     * @DataTime: 2021-07-07 22:47
     * @功能说明:收藏数量
     */
    public function getCollectNumAttr($value,$data){
        if(!empty($data['id'])){
            $comm_model = new CoachCollect();
            $num = $comm_model->where(['coach_id'=>$data['id']])->count();
            return $num;
        }
    }
    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2021-03-22 16:47
     */
    public function getIdCardAttr($value,$data){
        if(!empty($value)){
            return explode(',',$value);
        }
    }
    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2021-03-22 16:47
     */
    public function getLicenseAttr($value,$data){
        if(!empty($value)){
            return explode(',',$value);
        }
    }
    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2021-03-22 16:47
     */
    public function getSelfImgAttr($value,$data){
        if(!empty($value)){
            return explode(',',$value);
        }
    }
    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:37
     * @功能说明:后台列表
     */
    public function adminDataList($dis,$mapor,$page=10){
        $data = $this->alias('a')
                ->join('shequshop_school_user_list b','a.user_id = b.id')
                ->where($dis)
                ->where(function ($query) use ($mapor){
                    $query->whereOr($mapor);
                })
                ->field('a.*,b.nickName,b.avatarUrl')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();
        return $data;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-10-21 15:21
     * @功能说明:保留两位小数
     */
    public function getDistanceAttr($value){
        if(isset($value)){
            if($value>1000){
                $value = $value/1000;
                $value =  round($value,2);
                $value = $value.'km';
            }else{
                $value =  round($value,2);
                $value = $value.'m';
            }
            return $value;
        }
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){
        $data['create_time'] = time();
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){
        $res = $this->where($dis)->update($data);
        return $res;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10,$mapor=[]){
        $data = $this->where($dis)->where(function ($query) use ($mapor){
            $query->whereOr($mapor);
        })->order('id desc')->paginate($page)->toArray();
        return $data;
    }
    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis,$file='*'){
        $data = $this->where($dis)->field($file)->find();
        return !empty($data)?$data->toArray():[];
    }
    /**
     * @author chenniang
     * @DataTime: 2021-04-06 11:54
     * @功能说明:获取团长的openid
     */
    public function capOpenid($id,$type=2){
        if($type==1){
            $id = $this->alias('a')
                ->join('massage_service_user_list b','a.user_id = b.id')
                ->where(['a.id'=>$id])
                ->value('b.wechat_openid');
        }else{
            $id = $this->alias('a')
                ->join('massage_service_user_list b','a.user_id = b.id')
                ->where(['a.id'=>$id])
                ->value('b.web_openid');
        }
        return $id;
    }
    /**
     * @author chenniang
     * @DataTime: 2021-07-07 10:21
     * @功能说明:服务技师列表
     */
    public function serviceCoachList($dis,$alh,$page=10){
        $data = $this->alias('a')
                ->join('massage_service_service_coach b','a.id = b.coach_id','left')
                ->where($dis)
                ->field(['a.*',$alh])
                ->group('a.id')
                ->order('distance asc,a.id desc')
                ->paginate($page)
                ->toArray();
        return $data;
    }
    /**
     * @author chenniang
     * @DataTime: 2021-07-07 22:41
     * @功能说明:教练收藏列表
     */
    public function coachCollectList($dis,$alh,$page=10){
        $data = $this->alias('a')
                ->join('massage_service_coach_collect b','a.id = b.coach_id')
                ->where($dis)
                ->field(['a.*',$alh])
                ->order('distance asc,a.id desc')
                ->paginate($page)
                ->toArray();
        return $data;
    }
    /**
     * @author chenniang
     * @DataTime: 2021-07-08 09:58
     * @功能说明:获取技师等级
     */
    public function getCoachLevel($caoch_id,$uniacid){
        $level_model = new CoachLevel();
        $order_model = new Order();
        $dis = [
            'coach_id' => $caoch_id,
            'pay_type' => 7
        ];
        $time_long = $order_model->where($dis)->sum('true_time_long');
        $level = $level_model->where(['uniacid'=>$uniacid,'status'=>1])->order('time_long desc,id desc')->select()->toArray();
        if(!empty($level)){
            foreach ($level as $value){
                if($time_long<=$value['time_long']){
                    $coach_level = $value;
                }elseif(empty($coach_level)){
                    $coach_level = $value;
                }
            }
        }
        return !empty($coach_level)?$coach_level:'';
    }
    /**
     * @author chenniang
     * @DataTime: 2021-03-22 15:41
     * @功能说明:团长佣金到账
     */
    public function capArrPrice($uniacid,$cap_id=0){
        $dis[] = ['uniacid','=',$uniacid];
        $dis[] = ['pay_type','=',7];
        if(!empty($cap_id)){
            $dis[] = ['cap_id','=',$cap_id];
        }
        $dis[] = ['have_tx','=',0];
//        $dis[] = ['hx_time','<',time()-86400];
        $dis[] = ['hx_time','<',time()-60];
        $order = $this->where($dis)->field('id,cap_price,cap_id')->select()->toArray();
        if(!empty($order)){
            $cap_model = new Cap();
            $refund_model = new RefundOrder();
            foreach ($order as $value){
                //判断有无申请中的退款订单
                $refund_order = $refund_model->dataInfo(['order_id'=>$value['id'],'status'=>1]);
                if(empty($refund_order)){
                    //修改订单状态
                    $res = $this->where(['id'=>$value['id'],'have_tx'=>0])->update(['have_tx'=>1]);
                    //增加团长佣金
                    if($res==1){
                        $cap_cash = $value['cap_price'];
                        $cap_model->where(['id'=>$cap_id])->update(['cap_cash'=>Db::raw("cap_cash+$cap_cash")]);
                    }
                }
            }
        }
        return true;
    }
    /**
     * @author chenniang
     * @DataTime: 2021-04-01 09:51
     * @功能说明:公众号楼长端订单退款通知
     */
    public function refundSendMsgWeb($order){
        $cap_model = new Coach();
        $cap_id  = $order['coach_id'];
        $uniacid = $order['uniacid'];
        $x_config = longbingGetAppConfig($uniacid);
        if(empty($x_config['gzh_appid'])||empty($x_config['order_tmp_id'])){
            return false;
        }
        //获取楼长openid
        $openid = $cap_model->capOpenid($cap_id,2);
        $store_name = $cap_model->where(['id'=>$cap_id])->value('coach_name');
       // $access_token = longbingGetAccessToken($uniacid);
        $wx_setting = new WxSetting($uniacid);
        $access_token = $wx_setting->getGzhToken();
       // $page = "master/pages/order/list";
        //post地址
//        $url  = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token={$access_token}";
        $url  = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
        $order_model = new Order();
//        dump($order);exit;
        $start_time = $order_model->where(['id'=>$order['order_id']])->value('start_time');
        $data = [
            //用户小程序openid
            'touser' => $openid,
//            'mp_template_msg' => [
                //公众号appid
                'appid' => $x_config['gzh_appid'],
                "url"   => 'https://'.$_SERVER['HTTP_HOST'].'/h5/?#/technician/pages/order/detail?id='.$order['order_id'],
                //公众号模版id
                'template_id' => $x_config['cancel_tmp_id'],
//                'miniprogram' => [
//                    //小程序appid
//                    'appid'   => $x_config['appid'],
//                    //跳转小程序地址
//                    'page'    => $page,
//                ],
                'data' => array(
                    'first' => array(
                        'value' => $store_name.'技师，您有一笔订单正在申请退款',
                        'color' => '#93c47d',
                    ),
                    'keyword1' => array(
                        'value' => implode(',',array_column($order['order_goods'],'goods_name')),
                        'color' => '#93c47d',
                    ),
                    //预约时间
                    'keyword2' => array(
                        //内容
                        'value' => date('Y-m-d H:i',$start_time),
                        'color' => '#0000ff',
                    ),
                    //取消原因
                    'keyword3' => array(
                        //内容
                        'value' => $order['text'],
                        'color' => '#0000ff',
                    ),
                )
//            ],
        ];
        $data = json_encode($data);
        $tmp = [
            'url'  => $url,
            'data' => $data,
        ];
        $rest = lbCurlPost($tmp['url'], $tmp['data']);
        $rest = json_decode($rest, true);
        return $rest;
    }
    /**
     * @param $order
     * @功能说明:发送模版消息
     * @author chenniang
     * @DataTime: 2022-06-01 15:15
     */
    public function refundSendMsg($order){
        $coach_user_id = $this->where(['id'=>$order['coach_id']])->value('user_id');
        $user_model = new User();
        $user_info = $user_model->dataInfo(['id'=>$coach_user_id]);
        if(empty($user_info)){
            return false;
        }
        //type 1小程序 2公众号
        $type = $user_info['last_login_type']==0&&!empty($user_info['wechat_openid'])?1:2;
        if($type==1){
            $res = $this->refundSendMsgWechat($order);
        }else{
            $res = $this->refundSendMsgWeb($order);
        }
        $this->sendShortMsg($order,2);
        return $res;
    }
    /**
     * @author chenniang
     * @DataTime: 2021-04-01 09:51
     * @功能说明:公众号楼长端订单退款通知
     */
    public function refundSendMsgWechat($order){
        $cap_model = new Coach();
        $cap_id  = $order['coach_id'];
        $uniacid = $order['uniacid'];
        $x_config = longbingGetAppConfig($uniacid);
        if(empty($x_config['gzh_appid'])||empty($x_config['order_tmp_id'])){
            return false;
        }
        //获取楼长openid
        $openid = $cap_model->capOpenid($cap_id,1);
        $store_name = $cap_model->where(['id'=>$cap_id])->value('coach_name');
        $access_token = longbingGetAccessToken($uniacid);
        $page = "master/pages/order/list";
        //post地址
        $url  = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token={$access_token}";
        $order_model = new Order();
        $start_time = $order_model->where(['id'=>$order['order_id']])->value('start_time');
        $data = [
            //用户小程序openid
            'touser' => $openid,
            'mp_template_msg' => [
                //公众号appid
                'appid' => $x_config['gzh_appid'],
                "url"   => "http://weixin.qq.com/download",
                //公众号模版id
                'template_id' => $x_config['cancel_tmp_id'],
                'miniprogram' => [
                    //小程序appid
                    'appid'   => $x_config['appid'],
                    //跳转小程序地址
                    'page'    => $page,
                ],
                'data' => array(
                    'first' => array(
                        'value' => $store_name.'技师，您有一笔订单正在申请退款',
                        'color' => '#93c47d',
                    ),
                    'keyword1' => array(
                        'value' => implode(',',array_column($order['order_goods'],'goods_name')),
                        'color' => '#93c47d',
                    ),
                    //预约时间
                    'keyword2' => array(
                        //内容
                        'value' => date('Y-m-d H:i',$start_time),
                        'color' => '#0000ff',
                    ),
                    //取消原因
                    'keyword3' => array(
                        //内容
                        'value' => $order['text'],
                        'color' => '#0000ff',
                    ),
                )
            ],
        ];
        $data = json_encode($data);
        $tmp = [
            'url'  => $url,
            'data' => $data,
        ];
        $rest = lbCurlPost($tmp['url'], $tmp['data']);
        $rest = json_decode($rest, true);
        return $rest;
    }
    /**
     * @author chenniang
     * @DataTime: 2022-06-01 15:17
     * @功能说明:发送模版消息
     */
    public function paySendMsg($order){
        $coach_user_id = $this->where(['id'=>$order['coach_id']])->value('user_id');
        $user_model = new User();
        $user_info = $user_model->dataInfo(['id'=>$coach_user_id]);
        if(empty($user_info)){
            return false;
        }
        //type 1小程序 2公众号
        $type = $user_info['last_login_type']==0&&!empty($user_info['wechat_openid'])?1:2;
        if($type==1){
            $res = $this->paySendMsgWechat($order);
        }else{
            $res = $this->paySendMsgWeb($order);
        }
        //发送短信
        $this->sendShortMsg($order);
        return $res;
    }
    /**
     * @param $order
     * @功能说明:发送短信
     * @author chenniang
     * @DataTime: 2022-08-01 14:59
     */
    public function sendShortMsg($order,$type=1){
        $mobile = $this->where(['id'=>$order['coach_id']])->value('mobile');
        $config_model = new Config();
        $res = $config_model->sendSms($mobile,$order['uniacid'],$order['order_code'],$type);
        return $res;
    }
    /**
     * @author chenniang
     * @DataTime: 2021-04-01 09:51
     * @功能说明:公众号楼长端订单支付通知
     */
    public function paySendMsgWechat($order){
        $cap_model = new Coach();
        $cap_id  = $order['coach_id'];
        $uniacid = $order['uniacid'];
        $x_config = longbingGetAppConfig($uniacid);
        if(empty($x_config['gzh_appid'])||empty($x_config['order_tmp_id'])){
            return false;
        }
        //获取楼长openid
        $openid = $cap_model->capOpenid($cap_id,1);
        $store_name = $cap_model->where(['id'=>$cap_id])->value('coach_name');
        $user_model = new User();
        $mobile = $user_model->where(['id'=>$order['user_id']])->value('phone');
        $access_token = longbingGetAccessToken($uniacid);
        $page = "master/pages/order/list";
        //post地址
        $url  = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token={$access_token}";
        $data = [
            //用户小程序openid
            'touser' => $openid,
                'mp_template_msg' => [
                //公众号appid
                'appid' => $x_config['gzh_appid'],
                "url"   => "http://weixin.qq.com/download",
                //公众号模版id
                'template_id' => $x_config['order_tmp_id'],
                'miniprogram' => [
                    //小程序appid
                    'appid'   => $x_config['appid'],
                    //跳转小程序地址
                    'page'    => $page,
                ],
                'data' => array(
                    'first' => array(
                        'value' => $store_name.'技师，您有一笔新的订单',
                        'color' => '#93c47d',
                    ),
                    //服务名称
                    'keyword1' => array(
                        'value' => implode(',',array_column($order['order_goods'],'goods_name')),
                        'color' => '#93c47d',
                    ),
                    //下单人
                    'keyword2' => array(
                        //内容
                        'value' => $order['address_info']['user_name'],
                        'color' => '#0000ff',
                    ),
                    'keyword3' => array(
                        //内容
                        'value' => $order['address_info']['mobile'],
                        'color' => '#0000ff',
                    ),
                    //客户电话
                    'keyword4' => array(
                        //内容
                        'value' => $mobile,
                        'color' => '#0000ff',
                    ),
                    'keyword5' => array(
                        //内容
                        'value' => $order['address_info']['address'],
                        'color' => '#0000ff',
                    ),
                )
             ]
        ];
        $data = json_encode($data);
        $tmp = [
            'url'  => $url,
            'data' => $data,
        ];
        $rest = lbCurlPost($tmp['url'], $tmp['data']);
        $rest = json_decode($rest, true);
        return $rest;
    }
    /**
     * @author chenniang
     * @DataTime: 2021-04-01 09:51
     * @功能说明:公众号楼长端订单支付通知
     */
    public function paySendMsgWeb($order){
        $cap_model = new Coach();
        $cap_id  = $order['coach_id'];
        $uniacid = $order['uniacid'];
        $x_config = longbingGetAppConfig($uniacid);
        if(empty($x_config['gzh_appid'])||empty($x_config['order_tmp_id'])){
            return false;
        }
        //获取楼长openid
        $openid = $cap_model->capOpenid($cap_id,2);
        $store_name = $cap_model->where(['id'=>$cap_id])->value('coach_name');
        $wx_setting = new WxSetting($uniacid);
        $access_token = $wx_setting->getGzhToken();
        $user_model = new User();
        $mobile = $user_model->where(['id'=>$order['user_id']])->value('phone');
     //   $page = "master/pages/order/list";
        $url  = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
        $data = [
            //用户小程序openid
            'touser' => $openid,
            //公众号appid
            'appid' => $x_config['gzh_appid'],
//            "url"   => "http://weixin.qq.com/download",
            "url"   => 'https://'.$_SERVER['HTTP_HOST'].'/h5/?#/technician/pages/order/detail?id='.$order['id'],
            //公众号模版id
            'template_id' => $x_config['order_tmp_id'],
            'data' => array(
                'first' => array(
                    'value' => $store_name.'技师，您有一笔新的订单',
                    'color' => '#93c47d',
                ),
                //服务名称
                'keyword1' => array(
                    'value' => implode(',',array_column($order['order_goods'],'goods_name')),
                    'color' => '#93c47d',
                ),
                //下单人
                'keyword2' => array(
                    //内容
                    'value' => $order['address_info']['user_name'],
                    'color' => '#0000ff',
                ),
                'keyword3' => array(
                    //内容
                    'value' => $order['address_info']['mobile'],
                    'color' => '#0000ff',
                ),
                //客户电话
                'keyword4' => array(
                    //内容
                    'value' => $mobile,
                    'color' => '#0000ff',
                ),
                'keyword5' => array(
                    //内容
                    'value' => $order['address_info']['address'],
                    'color' => '#0000ff',
                ),
            )
        ];
        $data = json_encode($data);
        $tmp = [
            'url'  => $url,
            'data' => $data,
        ];
        $rest = lbCurlPost($tmp['url'], $tmp['data']);
        $rest = json_decode($rest, true);
        return $rest;
    }
    /**
     * @author chenniang
     * @DataTime: 2021-08-23 23:57
     * @功能说明:获取正在进行中的技师
     */
    public function getWorkingCoach($uniacid){
        $dis[] = ['uniacid','=',$uniacid];
        $dis[] = ['pay_type','in',[2,3,4,5,6]];
        $dis[] = ['start_time','<=',time()];
        $dis[] = ['end_time','>=',time()];
        $order = new Order();
        $data = $order->where($dis)->column('coach_id');
        return $data;
    }
    public function getCoachEarliestTime($coach_id,$config){
        $coach_model  = new Coach();
        $order_model = new Order();
        $coach  = $coach_model->dataInfo(['id'=>$coach_id]);
        if(empty($coach['start_time'])){
            return '';
        }
        $end_time   = strtotime($coach['end_time']);
        $start_time = strtotime($coach['start_time']);
        $end_time = $end_time>$start_time?$end_time:$end_time+86400;
        $i = 0;
        $data = [];
        $time = $start_time;
//        dump($coach['end_time'],$coach['start_time']);exit;
        while ($time<$end_time){
            $time = $start_time+$config['time_unit']*$i*60;
            //时间戳
            $data[$i]['time_str'] = $time;
            $data[$i]['time_text']= date('H:i',$time);
            $where   = [];
            $where[] = ['coach_id','=',$coach_id];
            $where[] = ['start_time','<=',$time];
            $where[] = ['end_time','>=',$time];
            $where[] = ['pay_type','not in',[-1]];
            $order = $order_model->dataInfo($where);
            if(!empty($order)){
                $data[$i]['status'] = 0;
            }else{
                $data[$i]['status'] = 1;
            }
            $data[$i]['status'] = $time<time()?0:$data[$i]['status'];
            if($data[$i]['status']==1){
                return $data[$i]['time_text'];
            }
            $i++;
        }
        return '';
    }
    /**
     * @author chenniang
     * @DataTime: 2021-08-24 18:06
     * @功能说明:获取教练最早可预约时间
     */
    public function getCoachEarliestTimeV2($coach_id,$config){
        $coach_model  = new Coach();
        $coach  = $coach_model->dataInfo(['id'=>$coach_id]);
        if(empty($coach['start_time'])){
            return '';
        }
        $min_time = strtotime($coach['start_time'])>time()?strtotime($coach['start_time']):time();
        $dis[] = ['coach_id','=',$coach_id];
        $dis[] = ['pay_type','in',[2,3,4,5,6]];
        $dis[] = ['start_time','<=',$min_time];
        $dis[] = ['end_time','>=',$min_time];
        $order = new Order();
        $data = $order->dataInfo($dis);
        if(!empty($data)){
            return date('H:i',$data['end_time']);
        }
        $now_time = strtotime(date('H',time()));
        //整点
        if($config['time_unit']==60){
            return date('H:i',$now_time+3600);
        }
        //查看是在上半时还是下半时
        $y = time()-$now_time;
        //下半时
        if($y>1800){
            return date('H:i',$now_time+1800);
        }else{
            return date('H:i',$now_time+3600);
        }
    }
    /**
     * @author chenniang
     * @DataTime: 2021-12-07 15:18
     * @功能说明:认证未认证的教练 通过电话号码
     */
    public function attestationCoach($user){
        if(!empty($user['phone'])){
            $dis = [
                'user_id' => 0,
                'mobile'  => $user['phone']
            ];
            $this->dataUpdate($dis,['user_id'=>$user['id']]);
        }
        return true;
    }
    /**
     * @author chenniang
     * @DataTime: 2021-04-01 09:51
     * @功能说明:技师修改结果通知
     */
    public function updateTmpWechat($cap_id,$uniacid,$type,$sh_text){
        $cap_model = new Coach();
        $x_config = longbingGetAppConfig($uniacid);
        if(empty($x_config['gzh_appid'])||empty($x_config['coachupdate_tmp_id'])){
            return false;
        }
        //获取楼长openid
        $openid = $cap_model->capOpenid($cap_id,1);
        $access_token = longbingGetAccessToken($uniacid);
        $page = "pages/mine?type=2";
        //post地址
        $url  = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token={$access_token}";
        $data = [
            //用户小程序openid
            'touser' => $openid,
            'mp_template_msg' => [
                //公众号appid
                'appid' => $x_config['gzh_appid'],
                "url"   => "http://weixin.qq.com/download",
                //公众号模版id
                'template_id' => $x_config['coachupdate_tmp_id'],
                'miniprogram' => [
                    //小程序appid
                    'appid'   => $x_config['appid'],
                    //跳转小程序地址
                    'page'    => $page,
                ],
                'data' => array(
                    'first' => array(
                        'value' => '技师修改审核通知',
                        'color' => '#93c47d',
                    ),
                    //服务名称
                    'keyword1' => array(
                        'value' => '平台' ,
                        'color' => '#93c47d',
                    ),
                    'keyword2' => array(
                        'value' => $type==2?'通过 '.$sh_text:'未通过 '.$sh_text ,
                        'color' => '#93c47d',
                    ),
                    'keyword3' => array(
                        'value' => date('Y-m-d H:i',time()),
                        'color' => '#93c47d',
                    )
                )
            ]
        ];
        $data = json_encode($data);
        $tmp = [
            'url'  => $url,
            'data' => $data,
        ];
        $rest = lbCurlPost($tmp['url'], $tmp['data']);
        $rest = json_decode($rest, true);
        return $rest;
    }
    /**
     * @author chenniang
     * @DataTime: 2021-04-01 09:51
     * @功能说明:技师修改结果通知
     */
    public function updateTmpWeb($cap_id,$uniacid,$type,$sh_text){
        $cap_model = new Coach();
        $x_config = longbingGetAppConfig($uniacid);
        if(empty($x_config['gzh_appid'])||empty($x_config['coachupdate_tmp_id'])){
            return false;
        }
        //获取楼长openid
        $openid = $cap_model->capOpenid($cap_id,2);
        $wx_setting = new WxSetting($uniacid);
        $access_token = $wx_setting->getGzhToken();
        $url  = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
        $data = [
            //用户小程序openid
            'touser' => $openid,
            //公众号appid
            'appid' => $x_config['gzh_appid'],
//            "url"   => "http://weixin.qq.com/download",
            "url"   => 'https://'.$_SERVER['HTTP_HOST'].'/h5/?#/pages/mine?type=2',
            //公众号模版id
            'template_id' => $x_config['coachupdate_tmp_id'],
            'data' => array(
                'first' => array(
                    'value' => '技师修改审核通知',
                    'color' => '#93c47d',
                ),
                //服务名称
                'keyword1' => array(
                    'value' => '平台' ,
                    'color' => '#93c47d',
                ),
                'keyword2' => array(
                    'value' => $type==2?'通过 '.$sh_text:'未通过 '.$sh_text ,
                    'color' => '#93c47d',
                ),
                'keyword3' => array(
                    'value' => date('Y-m-d H:i',time()),
                    'color' => '#93c47d',
                )
            )
        ];
        $data = json_encode($data);
        $tmp = [
            'url'  => $url,
            'data' => $data,
        ];
        $rest = lbCurlPost($tmp['url'], $tmp['data']);
        $rest = json_decode($rest, true);
        return $rest;
    }
    /**
     * @author chenniang
     * @DataTime: 2022-06-01 15:17
     * @功能说明:发送模版消息
     */
    public function updateSendMsg($coach_id,$status,$sh_text){
        $coach_user_id = $this->where(['id'=>$coach_id])->value('user_id');
        $user_model = new User();
        $user_info = $user_model->dataInfo(['id'=>$coach_user_id]);
        if(empty($user_info)){
            return false;
        }
        //type 1小程序 2公众号
        $type = $user_info['last_login_type']==0&&!empty($user_info['wechat_openid'])?1:2;
        if($type==1){
            $res = $this->updateTmpWechat($coach_id,$user_info['uniacid'],$status,$sh_text);
        }else{
            $res = $this->updateTmpWeb($coach_id,$user_info['uniacid'],$status,$sh_text);
        }
        return $res;
    }
}